<?php

class Counter extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{counter}}':
	 * @var double $id
	 * @var string $time
	 * @var integer $service_id
	 * @var integer $world_in
	 * @var integer $world_out
	 * @var integer $ukraine_in
	 * @var integer $ukraine_out
	 * @var string $cost
	 * @var integer $packet_id
	 * @var integer $is_aggregated
	 */
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{counter}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('time, service_id', 'required'),
			array('service_id, world_in, world_out, ukraine_in, ukraine_out, packet_id, is_aggregated', 'numerical', 'integerOnly'=>true),
			array('cost', 'length', 'max'=>12),
			array('balance', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, time, service_id, world_in, world_out, ukraine_in, ukraine_out, cost, packet_id, is_aggregated, time_from, time_to', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'packet'=>array(self::BELONGS_TO,'Packet','packet_id',
					'select'=>'id,name',),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','Id'),
			'time' => Yii::t('main','Time'),
			'service_id' => Yii::t('main','Service'),
			'world_in' => Yii::t('main','World In'),
			'world_out' => Yii::t('main','World Out'),
			'ukraine_in' => Yii::t('main','Ukraine In'),
			'ukraine_out' => Yii::t('main','Ukraine Out'),
			'cost' => Yii::t('main','Cost'),
			'packet_id' => Yii::t('main','Packet'),
			'is_aggregated' => Yii::t('main','Is Aggregated'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array('with'=>array('packet')));

		$criteria->compare('t.id',$this->id);

		$criteria->compare('t.service_id',$this->service_id);

		$criteria->compare('t.world_in',$this->world_in);

		$criteria->compare('t.world_out',$this->world_out);

		$criteria->compare('t.ukraine_in',$this->ukraine_in);

		$criteria->compare('t.ukraine_out',$this->ukraine_out);

		$criteria->compare('t.cost',$this->cost);

		$criteria->compare('t.packet_id',$this->packet_id);

		//$criteria->compare('is_aggregated',$this->is_aggregated);
		
		if (isset($this->time)) {
			if (strpos($this->time,'|')) {
				list($time_from,$time_to)=explode('|',$this->time);
				$criteria->addBetweenCondition('t.time', $time_from, $time_to);
			} else {
				$criteria->compare('t.time',$this->time,true);
			}
		} else {
			$time_from = date('Y-m-01');
			$time_to = date('Y-m-d H:i:s');
			$this->time=$time_from.'|'.$time_to;
			$criteria->addBetweenCondition('t.time', $time_from, $time_to);
		}
		
		return new CActiveDataProvider('Counter', array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>Setting::get('PerPage')),
			'sort'=>array(
				'defaultOrder'=>'t.time DESC',
			),
		));
	}
	
	public function afterFind()
	{
		$this->world_in=sprintf('%.3f',$this->world_in/1048576);
		$this->world_out=sprintf('%.3f',$this->world_out/1048576);
		$this->ukraine_in=sprintf('%.3f',$this->ukraine_in/1048576);
		$this->ukraine_out=sprintf('%.3f',$this->ukraine_out/1048576);
		$this->cost=sprintf('%.2f',$this->cost);
	}
	
    public function getTotals()		//FIXME сделать статический масив типа
    {
    	$criteria=$this->search()->criteria;
    	$criteria->with='';
    	$criteria->select=new CDbExpression('SUM(world_in) as world_in, SUM(world_out) as world_out, SUM(ukraine_in) as ukraine_in, SUM(ukraine_out) as ukraine_out, SUM(cost) as cost');
    	return $this->find($criteria);
    }
	
	public function behaviors()
    {
        return array(
            'DateTime' => array(
            	'class'=>'application.components.behaviors.DateTimeBehavior',
            ),
        );
    }
}